package com.sens.utils;

import android.graphics.Color;
import android.text.SpannableStringBuilder;

import com.sens.common.StringUtil;
import com.sens.common.TimeUtil;
import com.sens.database.GlobalSharedPreferences;
import com.sens.enums.PreferencesSetting;
import com.sens.ipc.mybank.application.Callback;
import com.sens.mybank.utils.SupportVersion;
import com.sens.orderback.OrderBackApplication;
import com.sens.vo.MybankOrderVo;

/**
 * 八步检测
 * 1分组 间隔
 * 2安装
 * 3版本
 * 4运行
 * 5ID
 * 6服务器
 * 7查询
 * 8连接
 */
public class MybankLogFilter implements Callback {
    private SpannableStringBuilder ssb = new SpannableStringBuilder("日志:");

    MybankLogFilter() {
        if (ssb.length() == 3) {
            String log = GlobalSharedPreferences.getInstance(OrderBackApplication.getInstance()).getString(PreferencesSetting.MYBANK_HOOK_LOG);
            if (log != null) {
                String lines[] = log.split("\n");
                for (String line : lines) {
                    if (line.length() == 0) continue;
                    ssb.append("\n");
                    switch (line) {
                        case "网商银行已安装":
                        case "网商银行已运行":
                        case "服务器地址已设置":
                        case "查询数据成功:":
                        case "服务器已连接":
                            ssb.append(StringUtil.createColorText(Color.BLUE, line));
                            break;
                        case "还未设置银行组":
                        case "网商银行未安装":
                        case "网商银行未运行":
                        case "未检测到网商银行用户ID，请主动登录网商银行":
                        case "服务器地址未设置":
                        case "查询数据失败":
                        case "服务器连接失败":
                            ssb.append(StringUtil.createColorText(Color.RED, line));
                            break;
                        case "可以正常使用了！":
                            ssb.append(StringUtil.createBiggerColorText(Color.BLUE, line));
                            break;
                        default: {
                            if (line.startsWith("银行组:")) {
                                ssb.append(StringUtil.createColorText(Color.BLUE, line));
                            } else if (line.startsWith("网商银行版本:")) {
                                String versionName = line.substring(7);
                                if (SupportVersion.checkSupportable(versionName))
                                    ssb.append(StringUtil.createColorText(Color.BLUE, line));
                                else
                                    ssb.append(StringUtil.createColorText(Color.RED, line));
                            } else if (line.startsWith("暂不支持当前网商银行版本,支持列表")) {
                                ssb.append(StringUtil.createColorText(Color.RED, line));
                            } else if (line.startsWith("网商银行用户ID:")) {
                                ssb.append(StringUtil.createColorText(Color.BLUE, line));
                            } else if (line.startsWith("账户信息:")) {
                                ssb.append(StringUtil.createColorText(Color.BLUE, line));
                            } else {
                                ssb.append(line);
                            }
                        }
                    }
                }
            }
        }
        noticeLog(ssb);
    }

    void saveLog() {
        GlobalSharedPreferences.getInstance(OrderBackApplication.getInstance()).putString(PreferencesSetting.MYBANK_HOOK_LOG, ssb.toString().substring(3));
    }

    void clearLog() {
        GlobalSharedPreferences.getInstance(OrderBackApplication.getInstance()).putString(PreferencesSetting.MYBANK_HOOK_LOG, (String) PreferencesSetting.MYBANK_HOOK_LOG.getDefaultValue());
        ssb.clear();
        ssb.append("日志:");
        noticeLog(ssb);
    }

    @Override
    public void noticeMessage(CharSequence message) {
        if (message == null) return;
        ssb.append("\n");
        ssb.append(message);
        noticeLog(ssb);
    }

    @Override
    public void updateUserId(String userId) {
        if (userId == null) return;
        ssb.append("\n");
        ssb.append(StringUtil.createColorText(Color.BLUE, "网商银行用户ID:" + userId));
        noticeLog(ssb);
    }

    @Override
    public void addOrder(MybankOrderVo mybankOrderVo) {
        if (mybankOrderVo == null) return;
        if (OrderBackApplication.getInstance().isMybankCollectionAble()) {
            ssb.append("\n");
            ssb.append("订单信息:" + mybankOrderVo);
            ssb.append("\n---------------" + TimeUtil.getFormatTime("yyyy-MM-dd HH:mm:ss") + "---------------");
            noticeLog(ssb);
        }
    }

    public void noticeLog(CharSequence message) {
    }
}
